const app = getApp()
Page({
  data: { 
    nvabarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '授权登录', //导航栏 中间的标题
    },
    height: app.globalData.height * 2 + 20,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: true, 
    phoneShow:false,
  },
  onLoad: function () {
    this.login();
  },
  login() {
    var that = this;
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo'] || !app.globalData.openId) {
          wx.login({
            success: res => {
              let code = res.code;
              let url = app.globalData.URL + 'wx/openId/get/'
              if (code) {
                wx.request({
                  url: url + code,
                  method: 'POST',
                  header: {
                    'content-type': 'application/json' // 默认值
                  },
                  success: function (res) {
                    console.log(res);
                    app.globalData.sessionKey = res.data.sessionKey;
                    app.globalData.openId = res.data.openId;
                    that.data.sessionKey = res.data.sessionKey;
                    that.data.openId = res.data.openId;
                    wx.setStorageSync({
                      key: 'openId',
                      openId: res.data.openId,
                      sessionKey: res.data.sessionKey
                    })
                  }
                })
              }
            }
          })
          // }
          // });
        } else {
          that.setData({
            isHide: true
          });
        }
      }
    });
  },
  bindGetUserInfo: function (e) {
    let that = this;
    that.login();
    if (e.detail.userInfo) {
      // 授权
      wx.getUserInfo({
        success: function (res) {
          wx.setStorageSync('userInfo', res.userInfo)
          let avatar = res.userInfo.avatarUrl;
          let nickName = res.userInfo.nickName;
          let sex = JSON.stringify(res.userInfo.gender);
          let openId = app.globalData.openId ? app.globalData.openId : that.data.openId;
          let sessionKey = that.data.sessionKey;
          let token = app.globalData.token;
          console.log('1:' + avatar, '2' + nickName, '3' + sex, '4' + openId, '5' + sessionKey)
          let url = app.globalData.URL + 'wx/login'
          wx.request({
            method: 'POST',
            url: url,
            data: {
              avatar: avatar,
              nickName: nickName,
              openId: openId,
              sessionKey: sessionKey,
              sex: sex
            },
            success: function (res) {
              console.log(res);
              getApp().globalData.token = res.data.data.token;
              wx.setStorageSync('token', res.data.data.token); 
              if (wx.getStorageSync('token')) {
                if(res.data.data.sysUser.phonenumber){
                  wx.switchTab({
                    url: '../index/index',
                  })
                }else{
                  that.setData({
                    phoneShow : true
                  }); 
                } 
              }
            },
          })
        }
      })
    } else {
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },
  hidePhoneMess:function(){
    this.setData({
      phoneShow : false
    });  
    wx.switchTab({
      url: '../index/index',
    })
  },
  getPhoneNumber: function(e) {  
    let that = this;  
    let url = app.globalData.URL + 'wx/getPhoneNumber'
    let  data= {
          openId: this.data.openId,
          encryptedData: e.detail.encryptedData,
          sessionKey:  this.data.sessionKey, 
          iv: e.detail.iv
      }
    that.setData({
      phoneShow : false
    }); 
    app.wxRequest('POST', url, data, (res) => {
      if(res.data.code == 200){ 
        
      }else{
        wx.showToast({ 
          title:"获取用户手机号码异常!",
          icon: 'none', 
          duration: 2000
        })
      }  
    })  
    wx.switchTab({
      url: '../index/index',
    })
  //   wx.request({
  //       url: url, //这里就写上后台解析手机号的接口 
  //       data: {
  //           openId: this.data.openId,
  //           encryptedData: e.detail.encryptedData,
  //           sessionKey:  this.data.sessionKey, 
  //           iv: e.detail.iv
  //       },
  //       method:'POST',
  //       header: {
  //           'content-type': "application/json" // POST请求
  //       },
  //       success (res) {
  //           if(res.data.code == 200){ 
  //             wx.switchTab({
  //               url: '../index/index',
  //             })
  //           }else{
  //             wx.showToast({ 
  //               title:"获取用户手机号码异常!",
  //               icon: 'none', 
  //               duration: 2000
  //             })
  //           } 
  //       },
  //       fail(){
  //         wx.showToast({ 
  //           title:"获取用户手机号码异常!",
  //           icon: 'none', 
  //           duration: 2000
  //         })
  //       }
  //  }) 
}
})